package com.example.zhaohang.employmentapp;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.zhaohang.been.ChengePhoneBeen;
import com.example.zhaohang.been.MesBeen;
import com.example.zhaohang.contect.UriContect;
import com.example.zhaohang.utils.GsonUtil;
import com.example.zhaohang.utils.ToastMake;
import com.example.zhaohang.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.MediaType;

public class ChengePhoneActivity extends BasicActivity implements View.OnClickListener {

    private EditText  mPhone;
    private Button    mButton;
    private EditText  mEidt;
    private Button    mSubmit;
    private int       mTime;
    private TimerTask mWait;
    private Timer timer = new Timer();
    private String mCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_chenge_phone);
        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setTitle_V("更换手机号");

        mPhone = (EditText) findViewById(R.id.name1);
        mPhone.setText(MyApplayCation.phone);

        mButton = (Button) findViewById(R.id.name5);
        mSubmit = (Button) findViewById(R.id.name8);
        mEidt = (EditText) findViewById(R.id.name3);

        mButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //发送验证码
            case R.id.name5:

                if (isConnected(ChengePhoneActivity.this)) {
                    inNumber();
                }
                break;
            //提交
            case R.id.name8:

                if (isConnected(ChengePhoneActivity.this)) {
                    exchenged();
                }

                break;
        }

    }

    private void exchenged() {
        String trim = mPhone.getText().toString().trim();
        String edit = mEidt.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            ToastMake.showToast(getResources().getString(R.string.name11));
            return;
        }
        if (trim.length() != 11) {
            ToastMake.showToast(getResources().getString(R.string.name12));
            return;
        }
        if (!Utils.isMobileNO(trim)) {
            return;
        }
        if (TextUtils.isEmpty(edit)) {
            ToastMake.showToast("验证码不能为空");
            return;
        }
        if (!TextUtils.equals(edit, mCode)) {
            ToastMake.showToast("验证码输入不正确,请重新输入");
            return;
        }
        Map<String, String> mapA = new HashMap<>();
        mapA.put("phone", MyApplayCation.phone);
        mapA.put("newphone", trim);
        mapA.put("code", mCode);
        final String jsonA = GsonUtil.parseMapToJson(mapA);
        new Thread(new Runnable() {
            @Override
            public void run() {
                showProgrees("提交中,请稍等...");
                OkHttpUtils
                        .postString()
                        .url(UriContect.CHANGE)
                        .content(jsonA)
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                ToastMake.showToast(getResources().getString(R.string.name18));
                                hideProgree();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                hideProgree();
                                ChengePhoneBeen been = GsonUtil.parseJsonToBean(response, ChengePhoneBeen.class);
                                if (been == null) {
                                    ToastMake.showToast(getResources().getString(R.string.name18));

                                    return;
                                }
                                if (!TextUtils.equals(been.getStatus(), "1")) {
                                    ToastMake.showToast(been.getMsg());

                                    return;
                                } else {
                                    ToastMake.showToast(been.getMsg());
                                    //修改完成结束
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            finish();
                                        }
                                    });

                                }

                            }
                        });
            }
        }).start();


    }

    private void inNumber() {

        final String trim = mPhone.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            ToastMake.showToast(getResources().getString(R.string.name11));
            return;
        }
        if (trim.length() != 11) {
            ToastMake.showToast(getResources().getString(R.string.name12));
            return;
        }
        if (!Utils.isMobileNO(trim)) {

            return;
        }
        if (TextUtils.equals(trim, MyApplayCation.phone)) {
            ToastMake.showToast("更换前后的电话号码不能为同一个电话号码");
            return;
        }
        mTime = 60;
        mWait = new TimerTask() {
            @Override
            public void run() {
                //再主线程中进行切换
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mTime <= 0) {
                            mPhone.setEnabled(true);
                            mPhone.setFocusable(true);
                            mPhone.setFocusableInTouchMode(true);
                            mPhone.requestFocus();
                            mButton.setText(getResources().getString(R.string.name19));
                            mButton.setEnabled(true);
                            mWait.cancel();
                        } else {
                            mPhone.setEnabled(false);
                            mPhone.setFocusable(false);
                            mPhone.setFocusableInTouchMode(false);
                            mButton.setText(mTime + "秒后重获");
                            mButton.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                        }
                        mTime--;

                    }
                });

            }
        };
        timer.schedule(mWait, 0, 1000);
        Map<String, String> map = new HashMap<>();
        map.put("newphone", trim);
        final String json = GsonUtil.parseMapToJson(map);

        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils
                        .postString()
                        .url(UriContect.SMS)
                        .content(json)
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                ToastMake.showToast(getResources().getString(R.string.name18));
                                mPhone.setEnabled(true);
                                mPhone.setFocusable(true);
                                mPhone.setFocusableInTouchMode(true);
                                mPhone.requestFocus();
                                mButton.setText(getResources().getString(R.string.name19));
                                mButton.setEnabled(true);
                                mWait.cancel();
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);

                                if (mesBeen == null) {
                                    ToastMake.showToast(getResources().getString(R.string.name18));
                                    mPhone.setEnabled(true);
                                    mPhone.setFocusable(true);
                                    mPhone.setFocusableInTouchMode(true);
                                    mPhone.requestFocus();
                                    mButton.setText(getResources().getString(R.string.name19));
                                    mButton.setEnabled(true);
                                    mWait.cancel();
                                    return;
                                }
                                if (!TextUtils.equals(mesBeen.getStatus(), "1")) {
                                    ToastMake.showToast(mesBeen.getMsg());
                                    mPhone.setEnabled(true);
                                    mPhone.setFocusable(true);
                                    mPhone.setFocusableInTouchMode(true);
                                    mPhone.requestFocus();
                                    mButton.setText(getResources().getString(R.string.name19));
                                    mButton.setEnabled(true);
                                    mWait.cancel();
                                    return;
                                } else {
                                    mCode = mesBeen.getCode();
                                }

                            }
                        });
            }
        }).start();
    }
}
